﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerAttack : MonoBehaviour
{
    public static PlayerAttack _instance;

    public bool isWaitEnemy = true;
    public int RoundIndex = 0;
    public int MainAttack = 0;
    //private Food HelpFood;
    private List<Food> HelpFooder = new List<Food>();
    private byte HelpAttacker = 0;
    private bool isDamageEneny = false;
    private GameObject finger;
    public int Combo;
    public int ComboHealth;

    //private List<Food> foodlist = new List<Food>();
    //private List<Food> attackList = new List<Food>();

    void Awake()
    {
        _instance = this;
    }

    /// <summary>
    /// 计算攻击顺序
    /// </summary>
    /// <returns></returns>
    //public List<Food> AttackOrder()
    //{
    //    foodlist.Clear();

    //    foreach (Food f in TeamManager._instance.PlayerFoods)
    //    {
    //        foodlist.Add(f);
    //    }
    //    Food food = foodlist[MainAttack];
    //    foodlist.RemoveAt(MainAttack);
    //    foodlist.Insert(0, food);

    //    return foodlist;
    //}

    /// <summary>
    /// 计算哪些食物要攻击
    /// </summary>
    //void CalcuAttackFoods()
    //{
    //    attackList.Clear();
    //    foreach (Food food in AttackOrder())
    //    {
    //        if (food.isAttack == true)
    //        {
    //            attackList.Add(food);
    //            food.isAttack = false;
    //        }

    //    }
    //}

    /// <summary>
    /// 响应按钮回合结束按钮
    /// </summary>
    //public void RoundOver()
    //{
    //    //CalcuAttackFoods();


    //    for (int i = 0; i < TeamManager._instance.PlayerFoods.Count; i++)
    //    {
    //        foreach (Food enemyfood in TeamManager._instance.EnemyFoods)
    //        {
    //            foreach (int a in TeamManager._instance.PlayerFoods[i].AttackRangeMapGridID)
    //            {
    //                int mapgridid = MapManager._instance.CalcuMapGridID(enemyfood.foodPosX, enemyfood.foodPosY);

    //                if (a == mapgridid)
    //                {
    //                    enemyfood.OnDamage(TeamManager._instance.PlayerFoods[i].foodInfo.AttackForce);
    //                    Debug.Log("在第" + RoundIndex + "回合" + "你的   " + TeamManager._instance.PlayerFoods[i].foodInfo.FoodName + "   对敌人的   " + enemyfood.foodInfo.FoodName + "   造成了" + TeamManager._instance.PlayerFoods[i].foodInfo.AttackForce + "点伤害");
    //                }
    //            }             
    //        }
    //    }

    //    RoundIndex++;
    //    MainAttack++;
    //    if (MainAttack >= 5)
    //        MainAttack = 0;

    //    TurnGame._instance.mState = OperatorState.EnemyRound;
    //    TurnGame._instance.GameControl();
    //}

    /// <summary>
    /// 播放攻击人特效
    /// </summary>
    public void PlayEffect()
    {
        //MapManager._instance.ClearMapGridState();
        TurnGame._instance.eventSystemGO.gameObject.SetActive(false);

        if (MainAttack >= TeamManager._instance.PlayerFoods.Count)
        {
            MainAttack = 0;

        }
        else
        {
            Debug.Log("玩家  " + MainAttack + TeamManager._instance.PlayerFoods[MainAttack].foodInfo.FoodName);
            if (TeamManager._instance.PlayerFoods[MainAttack].foodGo == null)
            {
                Debug.Log("玩家  "+TeamManager._instance.PlayerFoods[MainAttack].foodInfo.FoodName + "  为空");
                RoundIndex++;
                MainAttack++;
                PlayEffect();
            }
            else
            {
                MapManager._instance.HideRange(TeamManager._instance.PlayerFoods[PlayerAttack._instance.MainAttack]);
                string tag = TeamManager._instance.PlayerFoods[MainAttack].foodGo.tag;
                SkillManager._instance.UseSkill(TeamManager._instance.PlayerFoods[MainAttack], tag);
            }        
        }
    }

    /// <summary>
    /// 一次性播放受到的伤害
    /// </summary>s
    public void PlayDamage()
    {
        //造成伤害
        for (int i = 0; i < TeamManager._instance.EnemyFoods.Count; i++)
        {
            //Debug.Log(i);
            if (TeamManager._instance.EnemyFoods[i].foodGo != null)
            {
                foreach (int a in TeamManager._instance.PlayerFoods[MainAttack].AttackRangeMapGridID)
                {

                    //Debug.Log(TeamManager._instance.EnemyFoods[i].foodPosX+ " " +TeamManager._instance.EnemyFoods[i].foodPosY);

                    int mapgridid = MapManager._instance.CalcuMapGridID(TeamManager._instance.EnemyFoods[i].foodPosX, TeamManager._instance.EnemyFoods[i].foodPosY);

                    if (a == mapgridid)
                    {
                        isDamageEneny = true;
                        Combo++;
                        Debug.Log("在第" + RoundIndex + "回合" + "你的   " + TeamManager._instance.PlayerFoods[MainAttack].foodInfo.FoodName + "   对敌人的   " + TeamManager._instance.EnemyFoods[i].foodInfo.FoodName + "   造成了" + TeamManager._instance.PlayerFoods[MainAttack].foodInfo.AttackForce + "点伤害");             
                        TeamManager._instance.EnemyFoods[i].OnDamage(TeamManager._instance.PlayerFoods[MainAttack].foodInfo.AttackForce);
                        ComboHealth += TeamManager._instance.PlayerFoods[MainAttack].foodInfo.AttackForce;
                    }
                }
            }
        }
        HealthInfoView._instance.UpdateHealth();

        if (isDamageEneny)
        {
            TeamManager._instance.PlayerFoods[MainAttack].Energy++;
            TeamManager._instance.PlayerFoods[MainAttack].foodView.SetEnergyView(TeamManager._instance.PlayerFoods[MainAttack].Energy);
        }
        isDamageEneny = false;

        AddEnergy();
    }

    /// <summary>
    /// 增加生命
    /// </summary>
    public void AddHealth()
    {
        foreach (Food food in TeamManager._instance.PlayerFoods)
        {
            foreach (int mapgridid in TeamManager._instance.PlayerFoods[MainAttack].AttackRangeMapGridID)
            {
                int temp = MapManager._instance.CalcuMapGridID(food.foodPosX, food.foodPosY);

                if (temp == mapgridid)
                {
                    TeamManager._instance.CurrenPlayerHealth += TeamManager._instance.PlayerFoods[MainAttack].foodInfo.AttackForce;
                    HealthInfoView._instance.UpdateHealth();
                    food.foodView.ShowDamage(TeamManager._instance.PlayerFoods[MainAttack].foodInfo.AttackForce.ToString(), Color.green);
                }
            }
        }
        AddEnergy();
    }

    public void AddEnergy()
    {
        //添加能量
        foreach (Food food in TeamManager._instance.PlayerFoods)
        {
            foreach (int mapgridid in TeamManager._instance.PlayerFoods[MainAttack].AttackRangeMapGridID)
            {
                int temp = MapManager._instance.CalcuMapGridID(food.foodPosX, food.foodPosY);

                if (temp == mapgridid)
                {

                    Debug.Log(food.foodInfo.FoodName + "能量增加1点");
                    HelpFooder.Add(food);
                }
            }
        }

        if (HelpFooder != null)
        {
            //Debug.Log("帮助攻击开始__________" + HelpFooder.Count);
            FoodHelper();
        }
        else
        {
            if (TeamManager._instance.CurrenEnemyHealth <= 0)
            {
                TurnGame._instance.mState = OperatorState.EnemyQuit;
                TurnGame._instance.GameControl();

            }
            else
            {
                StartCoroutine("GoOnAttack");
            }
        }
    }

    private void FoodHelper()
    {
        if (HelpAttacker < HelpFooder.Count)
        {
            //Debug.Log("判断帮助LIST里面是不是空        " + HelpFooder[HelpAttacker].foodGo.name);
            string tag = "PlayerHelp";
            HelpFooder[HelpAttacker].foodView.PlayerAttackEffect(HelpFooder[HelpAttacker].AttackRangeMapGridID, tag);
        }
        else
        {
            //Debug.Log("判断帮助LIST里面是不是空        为空" + HelpFooder[HelpAttacker].foodGo.name);
            HelpAttacker = 0;
            HelpFooder.Clear();
            if (TeamManager._instance.CurrenEnemyHealth <= 0)
            {
                TurnGame._instance.mState = OperatorState.EnemyQuit;
                TurnGame._instance.GameControl();
            }
            else
            {
                StartCoroutine("GoOnAttack");
            }
        }

    }

    public void HelpDamage()
    {
        for (int i = 0; i < TeamManager._instance.EnemyFoods.Count; i++)
        {
            //Debug.Log(i);
            if (TeamManager._instance.EnemyFoods[i].foodGo != null)
            {
                foreach (int a in HelpFooder[HelpAttacker].AttackRangeMapGridID)
                {

                    //Debug.Log(TeamManager._instance.EnemyFoods[i].foodPosX+ " " +TeamManager._instance.EnemyFoods[i].foodPosY);

                    int mapgridid = MapManager._instance.CalcuMapGridID(TeamManager._instance.EnemyFoods[i].foodPosX, TeamManager._instance.EnemyFoods[i].foodPosY);

                    if (a == mapgridid)
                    {
                        isDamageEneny = true;
                        Combo++;
                        Debug.Log("  【帮助攻击】    " + "你的   " + HelpFooder[HelpAttacker].foodInfo.FoodName + "   对敌人的   " + TeamManager._instance.EnemyFoods[i].foodInfo.FoodName + "   造成了" + HelpFooder[HelpAttacker].foodInfo.AttackForce + "点伤害");
                        TeamManager._instance.EnemyFoods[i].OnDamage(HelpFooder[HelpAttacker].foodInfo.AttackForce);
                        ComboHealth += HelpFooder[HelpAttacker].foodInfo.AttackForce;
                    }
                }
            }
        }
        HealthInfoView._instance.UpdateHealth();

        if (isDamageEneny)
        {
            HelpFooder[HelpAttacker].Energy++;
            HelpFooder[HelpAttacker].foodView.SetEnergyView(HelpFooder[HelpAttacker].Energy);
        }
        isDamageEneny = false;

        HelpAttacker++;
        FoodHelper();
    }

    public void HelpHealth()
    {
        foreach (Food food in TeamManager._instance.PlayerFoods)
        {
            foreach (int mapgridid in HelpFooder[HelpAttacker].AttackRangeMapGridID)
            {
                int temp = MapManager._instance.CalcuMapGridID(food.foodPosX, food.foodPosY);

                if (temp == mapgridid)
                {
                    TeamManager._instance.CurrenPlayerHealth += HelpFooder[HelpAttacker].foodInfo.AttackForce;
                    HealthInfoView._instance.UpdateHealth();
                    food.foodView.ShowDamage(HelpFooder[HelpAttacker].foodInfo.AttackForce.ToString(), Color.green);
                }
            }
        }
        HelpAttacker++;
        FoodHelper();
    }

    IEnumerator GoOnAttack()
    {
        yield return new WaitForSeconds(1f);
        RoundIndex++;
        MainAttack++;
        if (MainAttack >= TeamManager._instance.PlayerFoods.Count)
        {
            MainAttack = 0;
        }
        SkillManager._instance.BuffClear();   
        Destroy(finger);
        Combo = 0;
        ComboHealth = 0;
        TurnGame._instance.mState = OperatorState.EnemyRound;
        TurnGame._instance.GameControl();
    }


    public void AssignAttackFood()
    {
        finger = Instantiate(Resources.Load("Prefabs/Fighter")) as GameObject;
        finger.transform.SetParent(TeamManager._instance.PlayerFoods[MainAttack].foodGo.transform);
        //finger.transform.localPosition = new Vector3(5f, 65f, 0);
        finger.transform.localScale = Vector3.one;
    }
}

